/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.service.impl;

import me.zhengjie.domain.FhInfo;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.repository.FhInfoRepository;
import me.zhengjie.service.FhInfoService;
import me.zhengjie.service.dto.FhInfoDto;
import me.zhengjie.service.dto.FhInfoQueryCriteria;
import me.zhengjie.service.mapstruct.FhInfoMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author admin
* @date 2020-07-23
**/
@Service
@RequiredArgsConstructor
public class FhInfoServiceImpl implements FhInfoService {

    private final FhInfoRepository fhInfoRepository;
    private final FhInfoMapper fhInfoMapper;

    @Override
    public Map<String,Object> queryAll(FhInfoQueryCriteria criteria, Pageable pageable){
        Page<FhInfo> page = fhInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(fhInfoMapper::toDto));
    }

    @Override
    public List<FhInfoDto> queryAll(FhInfoQueryCriteria criteria){
        return fhInfoMapper.toDto(fhInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public FhInfoDto findById(Integer id) {
        FhInfo fhInfo = fhInfoRepository.findById(id).orElseGet(FhInfo::new);
        ValidationUtil.isNull(fhInfo.getId(),"FhInfo","id",id);
        return fhInfoMapper.toDto(fhInfo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public FhInfoDto create(FhInfo resources) {
        return fhInfoMapper.toDto(fhInfoRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(FhInfo resources) {
        FhInfo fhInfo = fhInfoRepository.findById(resources.getId()).orElseGet(FhInfo::new);
        ValidationUtil.isNull( fhInfo.getId(),"FhInfo","id",resources.getId());
        fhInfo.copy(resources);
        fhInfoRepository.save(fhInfo);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            fhInfoRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<FhInfoDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (FhInfoDto fhInfo : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("简介", fhInfo.getIntro());
            map.put("土地面积", fhInfo.getLandArea());
            map.put("建筑面积", fhInfo.getArchitectArea());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}